package com.springboot.demo.common.result;

import java.util.HashMap;

import com.springboot.demo.common.enums.Status;
import com.springboot.demo.common.exception.BusinessException;

/**
 * @ClassName:JsonResult.java
 * @Package com.springboot.demo.common.result
 * @Description:用一句话描述该文件做什么
 * @Author JuoChiu
 * @Date  2018年2月9日上午9:54:26
 * @Version
 */
public class JsonResult extends HashMap<String, Object>{
	 /**
     * 结果状态
     */
    protected Status status;

    /**
     * 信息码
     */
    protected Integer code;

    /**
     * 描述
     */
    protected String description;

	private static final long serialVersionUID = 1L;
	final String SUCCESSCODE = "0";
	final String SUCCESSMSG = "ok";
	final String FAILEDCODE = "400";
	final String CODE = "0";
	final String SUCCESS = "执行成功";
	final String FAILE = "执行失败";
	
	/**
	 * 返回结果集, 无记录条数, 和成功/错误信息+errCode
	 * @param object	结果集
	 * @param Code
	 * @param Msg	
	 * @return
	 */
	public JsonResult put(Object object, String errCode, String errMsg){
		super.put("data", object);
		super.put("code", errCode);
		super.put("message", errMsg);
		return this;
	}
	
	/**
	 * 返回结果集, 带记录条数
	 * @param object	结果集
	 * @param num		总记录条数
	 * @return
	 */
	public JsonResult put(Object object , String num) {
		super.put("data", object);
		super.put("count", num);
		super.put("code", CODE);
		super.put("message", SUCCESS);
		return this;
	}
	
	/**
	 * 返回结果集, 无记录条数
	 * @param object	结果集
	 * @return
	 */
	public JsonResult put(Object object){
		super.put("data", object);
		super.put("code", CODE);
		super.put("message", SUCCESS);
		return this;
	}
	
	/**
	 * 返回成功/错误信息+errCode
	 * @param errMsg	结果集
	 * @param errCode	errorcode
	 * @return
	 */
	public JsonResult put(String errMsg, String errCode){
		super.put("code", errCode);
		super.put("message", errMsg);
		return this;
	}
	
	/**
	 * 返回成功信息+errCode
	 * @param errMsg	结果集
	 * @param errCode	errorcode
	 * @return
	 */
	public JsonResult Success(){
		super.put("code", CODE);
		return this;
	}
	
	/**
	 * @param object
	 * @return
	 */
	public JsonResult putSuccess(Object object){
		super.put("data", object);
		super.put("code", CODE);
		super.put("message", SUCCESS);
		return this;
	}
	
	/**
	 * @param object
	 * @return
	 */
	public JsonResult putSuccess(Object object,String message){
		super.put("data", object);
		super.put("code", CODE);
		super.put("message", message);
		return this;
	}
	
	public JsonResult putSuccess(){
		super.put("code", CODE);
		super.put("message", SUCCESS);
		return this;
	}
	
	/**
	 * 返回失败信息+errCode
	 * @param errMsg	结果集
	 * @param errCode	errorcode
	 * @return
	 */
	public JsonResult putFailed(String errMsg){
		super.put("code", FAILEDCODE);
		super.put("message", errMsg);
		return this;
	}
	
	public JsonResult putFailed(){
		super.put("code", FAILEDCODE);
		super.put("message", FAILE);
		return this;
	}
	
	 /**
     * 通过BusinessException创建一个BaseResult
     *
     * @param ex    BusinessException
     * @return
     */
    public void EXCEPTION(BusinessException ex) {
        this.status = Status.FAIL;
        this.setCode(ex.getCode());
        this.setDescription(ex.getMessage());
    }

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
    
}
